package com.coffee.admin.controller.platform;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.controller.BaseController;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.anotation.PlatformLoginCheck;
import com.coffee.core.anotation.SupplierLoginCheck;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.SkuTypeEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.order.api.inquire.service.InquireOrderTestingService;
import com.coffee.order.api.sku.service.EnjoyBeforePaySkuService;
import com.coffee.order.api.sku.service.PeriodizationSkuService;
import com.coffee.sku.api.inquire.rep.InquireSkuListRep;
import com.coffee.sku.api.inquire.result.InquireSkuContentResult;
import com.coffee.sku.api.inquire.service.InquireSkuContentService;
import com.coffee.sku.api.sku.req.AddSkuSortReq;
import com.coffee.sku.api.sku.req.SkuListReq;
import com.coffee.sku.api.sku.result.PeriodizationSkuDetailResult;
import com.coffee.sku.api.sku.result.PeriodizationSkuListResult;
import com.coffee.sku.api.sku.result.SkuContentResult;
import com.coffee.sku.api.sku.result.SkuListResult;
import com.coffee.sku.api.sku.service.RecommendSkuService;
import com.coffee.sku.api.sku.service.SkuContentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/platform/sku")
public class PlatformSkuController extends BaseController {

    @DubboReference(check = false)
    private SkuContentService skuContentService;
    @DubboReference(check = false)
    private InquireSkuContentService inquireSkuContentService;
    @DubboReference(check = false)
    private InquireOrderTestingService inquireOrderTestingService;
    @DubboReference(check = false)
    private EnjoyBeforePaySkuService enjoyBeforePaySkuService;
    @DubboReference(check = false)
    private PeriodizationSkuService periodizationSkuService;
    @DubboReference(check = false)
    private RecommendSkuService recommendSkuService;


    @OperLog(operModul = "平台-商品管理-商品管理", operDesc = "分页查询商品管理列表")
    @PlatformLoginCheck(alias = "PlatformSkuListAlias")
    @RequestMapping(value = "/findList", method = {RequestMethod.POST})
    public Result<Page<SkuListResult>> findList(HttpServletRequest request, @RequestBody SkuListReq params) {
        Result<Page<SkuListResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        params.setSkuType(SkuTypeEnum.SKU.getCode());
        Integer status = params.getStatus();
        if (status != null && status == 0) {
            params.setIsApprove("W");
        } else if (status != null && status == 1) {
            params.setIsApprove("N");
        } else if (status != null && status == 2) {
            params.setIsApprove("Y");
        } else if (status != null && status == 3) {
            params.setIsEnable("N");
        } else if (status != null && status == 4) {
            params.setIsEnable("Y");
        }
        params.setIsDelete("N");
        result.setData(skuContentService.findList(params, pagerInfo));
        return result;
    }

    @OperLog(operModul = "平台-商品管理-商品启用禁用", operDesc = "商品启用禁用")
    @PlatformLoginCheck(alias = "PlatformSkuOperationAlias")
    @RequestMapping(value = "/updateIsDisable", method = {RequestMethod.POST})
    public Result<String> updateIsDisable(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String isDisable = jsonObject.getString("isDisable");
        Result<String> result = Result.success(null);
        skuContentService.updateIsDisable(id, isDisable, SkuTypeEnum.SKU.getCode());
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-商品管理-商品审核", operDesc = "商品审核")
    @PlatformLoginCheck(alias = "PlatformSkuOperationAlias")
    @RequestMapping(value = "/updateSort", method = {RequestMethod.POST})
    public Result<String> updateSort(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        JSONArray addSkuSortReq = jsonObject.getJSONArray("addSkuSortReq");
        List<AddSkuSortReq> addSkuSortReqs = JSONObject.parseArray(JSONObject.toJSONString(addSkuSortReq), AddSkuSortReq.class);
        Integer categoryId = jsonObject.getInteger("categoryId");
        Result<String> result = Result.success(null);
        skuContentService.updateSort(addSkuSortReqs, categoryId);
        result.setData("SUCCESS");
        return result;
    }


    @OperLog(operModul = "平台-商品管理-商品删除", operDesc = "商品删除")
    @PlatformLoginCheck(alias = "PlatformSkuOperationAlias")
    @RequestMapping(value = "/delete")
    public Result<String> delete(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("skuId");
        Result<String> result = Result.success(null);
        skuContentService.delete(id);
        result.setData("SUCCESS");
        return result;
    }


    @OperLog(operModul = "平台-商品管理-商品详情", operDesc = "商品详情")
    @PlatformLoginCheck(alias = "PlatformSkuOperationAlias")
    @RequestMapping(value = "/detail")
    public Result<SkuContentResult> detail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("skuId");
        Result<SkuContentResult> result = Result.success(null);
        SkuContentResult detail = null;
        try {
            detail = skuContentService.getDetail(id);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData(detail);
        return result;
    }


    @OperLog(operModul = "平台-商品管理-期货商品", operDesc = "分页查询期货商品列表")
    @PlatformLoginCheck(alias = "PlatformInquireSkuListAlias")
    @RequestMapping(value = "/inquire/findList", method = {RequestMethod.POST})
    public Result<Page<InquireSkuListRep>> inquireFindList(HttpServletRequest request, @RequestBody SkuListReq params) {
        Result<Page<InquireSkuListRep>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        result.setData(inquireSkuContentService.findList(params, pagerInfo));
        return result;
    }


    @OperLog(operModul = "平台-期货商品管理-商品审核", operDesc = "商品审核")
    @PlatformLoginCheck(alias = "PlatformSkuOperationAlias")
    @RequestMapping(value = "/updateIsApprove", method = {RequestMethod.POST})
    public Result<String> updateIsApprove(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("skuId");
        String isApprove = jsonObject.getString("isApprove");
        String approveRefuseReason = jsonObject.getString("approveRefuseReason");
        Result<String> result = Result.success(null);
        skuContentService.updateIsApprove(id, isApprove, approveRefuseReason, SkuTypeEnum.SKU.getCode());
        result.setData("SUCCESS");
        return result;
    }


    @OperLog(operModul = "平台-期货商品管理-商品审核", operDesc = "商品审核")
    @PlatformLoginCheck(alias = "PlatformInquireSkuOperationAlias")
    @RequestMapping(value = "/inquire/updateIsApprove", method = {RequestMethod.POST})
    public Result<String> updateInquireIsApprove(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("skuId");
        String isApprove = jsonObject.getString("isApprove");
        String approveRefuseReason = jsonObject.getString("approveRefuseReason");
        Result<String> result = Result.success(null);
        skuContentService.updateIsApprove(id, isApprove, approveRefuseReason, SkuTypeEnum.INQUIRE.getCode());
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-期货商品管理-商品启用禁用", operDesc = "商品启用禁用")
    @PlatformLoginCheck(alias = "PlatformInquireSkuOperationAlias")
    @RequestMapping(value = "/inquire/updateIsDisable", method = {RequestMethod.POST})
    public Result<String> updateInquireIsDisable(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String isDisable = jsonObject.getString("isDisable");
        Result<String> result = Result.success(null);
        skuContentService.updateIsDisable(id, isDisable, SkuTypeEnum.INQUIRE.getCode());
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-期货商品管理-商品详情", operDesc = "期货商品详情")
    @PlatformLoginCheck(alias = "PlatformInquireSkuListAlias")
    @RequestMapping(value = "/inquire/detail", method = {RequestMethod.POST})
    public Result<InquireSkuContentResult> inquireDetail(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer skuId = jsonObject.getInteger("skuId");
        Result<InquireSkuContentResult> result = Result.success(null);
        InquireSkuContentResult detail = null;
        try {
            detail = inquireSkuContentService.getDetail(skuId, null);
        } catch (BusinessException e) {
            e.printStackTrace();
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData(detail);
        return result;
    }

    @OperLog(operModul = "平台-商品管理-添加热销商品", operDesc = "添加热销商品")
    @PlatformLoginCheck(alias = "PlatformCommendSkuOperationAlias")
    @RequestMapping(value = "/recommend/add", method = {RequestMethod.POST})
    public Result<String> enjoySku(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer cityId = jsonObject.getInteger("cityId");
        JSONArray skuIdJSONArray = jsonObject.getJSONArray("skuIds");
        List<Integer> skuIds = JSONObject.parseArray(skuIdJSONArray.toJSONString(), Integer.class);
        Result<String> result = Result.success(null);
        try {
            recommendSkuService.addBySkuIds(skuIds, cityId);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-商品管理-热销商品排序", operDesc = "热销商品排序")
    @PlatformLoginCheck(alias = "PlatformCommendSkuOperationAlias")
    @RequestMapping(value = "/recommend/sort", method = {RequestMethod.POST})
    public Result<String> recommendSort(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        JSONArray skuIdJSONArray = jsonObject.getJSONArray("skuIds");
        Integer cityId = jsonObject.getInteger("cityId");
        List<Integer> skuIds = JSONObject.parseArray(skuIdJSONArray.toJSONString(), Integer.class);
        Result<String> result = Result.success(null);
        try {
            recommendSkuService.changSort(skuIds, cityId);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-商品管理-删除先享后付商品", operDesc = "删除先享后付商品")
    @PlatformLoginCheck(alias = "PlatformCommendSkuOperationAlias")
    @RequestMapping(value = "/recommend/delete", method = {RequestMethod.POST})
    public Result<String> enjoySkuDelete(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        Integer skuId = jsonObject.getInteger("skuId");
        Result<String> result = Result.success(null);
        recommendSkuService.deleteBySkuId(skuId);
        result.setData("SUCCESS");
        return result;
    }

    @OperLog(operModul = "平台-商品管理-商品管理", operDesc = "分页查询商品管理列表")
    @PlatformLoginCheck(alias = "PlatformCommendSkuListAlias")
    @RequestMapping(value = "/recommend/findList", method = {RequestMethod.POST})
    public Result<List<SkuListResult>> recommendFindList(HttpServletRequest request, @RequestBody SkuListReq params) {
        Result<List<SkuListResult>> result = Result.success(null);
        params.setSkuType(SkuTypeEnum.SKU.getCode());
        params.setIsApprove("Y");
        params.setIsEnable("Y");
        params.setIsDelete("N");
        result.setData(recommendSkuService.getList(params));
        return result;
    }

    @OperLog(operModul = "供应商-商品管理-商品管理", operDesc = "分页查询商品管理列表")
    @PlatformLoginCheck(alias = "PlatformSkuListAlias")
    @RequestMapping(value = "/enjoy/findList", method = {RequestMethod.POST})
    public Result<Page<SkuListResult>> enjoyFindList(HttpServletRequest request, @RequestBody SkuListReq params) {
        Result<Page<SkuListResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        params.setSkuType(SkuTypeEnum.SKU.getCode());
        params.setIsApprove("Y");
        params.setIsEnable("Y");
        params.setIsDelete("N");
        result.setData(enjoyBeforePaySkuService.getPager(params, pagerInfo));
        return result;
    }


    @OperLog(operModul = "供应商-商品管理-周期购商品列表", operDesc = "分页查询周期购商品列表")
    @PlatformLoginCheck(alias = "PlatformPeriodizationSkuListAlias")
    @RequestMapping(value = "/periodization/findList", method = {RequestMethod.POST})
    public Result<Page<PeriodizationSkuListResult>> periodizationFindList(HttpServletRequest request, @RequestBody SkuListReq params) {
        Result<Page<PeriodizationSkuListResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(params.getPageSize(), params.getPageIndex());
        params.setSkuType(SkuTypeEnum.SKU.getCode());
        params.setIsApprove("Y");
        params.setIsEnable("Y");
        params.setIsDelete("N");
        result.setData(periodizationSkuService.getPager(params, pagerInfo));
        return result;
    }

    @OperLog(operModul = "供应商-商品管理-查询周期购商品详情", operDesc = "查询周期购商品详情")
    @PlatformLoginCheck(alias = "PlatformPeriodizationSkuListAlias")
    @RequestMapping(value = "/periodization/getDetail", method = {RequestMethod.POST})
    public Result<PeriodizationSkuDetailResult> getDetail(HttpServletRequest request, @RequestBody SkuListReq params) {
        Result<PeriodizationSkuDetailResult> result = Result.success(null);
        result.setData(periodizationSkuService.getDetail(params.getSkuId()));
        return result;
    }


}
